package cn.phecda.boot.data.mybatis.plus;

import cn.phecda.boot.data.core.entity.DataStatus;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * <p> 审计元数据处理器 </p>
 *
 * @author : wanpinwei
 */
public class EntityMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Date now = new Date();
        String username = getCurrentUsername();
        this.strictInsertFill(metaObject, "createdDate", Date.class, now); // 创建时间
        this.strictInsertFill(metaObject, "createdBy", String.class, username); // 创建者
        this.strictInsertFill(metaObject, "lastModifiedDate", Date.class, now); // 更新时间
        this.strictInsertFill(metaObject, "lastModifiedBy", String.class, username); // 更新者
        this.strictInsertFill(metaObject, "dataStatus", DataStatus.class, DataStatus.ENABLE);
        this.strictInsertFill(metaObject, "reserved", Boolean.class, false);
        this.strictInsertFill(metaObject, "sortNum", Integer.class, 0);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "lastModifiedDate", Date.class, new Date()); // 更新时间
        this.strictUpdateFill(metaObject, "lastModifiedBy", String.class, getCurrentUsername()); // 更新者
        this.strictUpdateFill(metaObject, "dataStatus", DataStatus.class, DataStatus.ENABLE);
        this.strictUpdateFill(metaObject, "reserved", Boolean.class, false);
        this.strictUpdateFill(metaObject, "sortNum", Integer.class, 0);
    }

    private String getCurrentUsername() {
        // TODO 实现获取当前用户ID的逻辑，例如从session中获取或通过认证信息获取
        return "unknown";
    }
}
